
# include "dfxKeyboard_Win32.h"
# include "dfxWindow_Win32.h"

namespace DFX
{
    Keyboard_Win32::Keyboard_Win32(Window *w)
    {
        memset(mKeyStates, 0, sizeof(bool)* KEYBOARD_KEY_MAX);
        memset(mHoldStates, 0, sizeof(bool)* KEYBOARD_KEY_MAX);

        mRID.usUsagePage = HID_USAGE_PAGE_GENERIC;
        mRID.usUsage = HID_USAGE_GENERIC_KEYBOARD;
        mRID.dwFlags = RIDEV_INPUTSINK;
        mRID.hwndTarget = static_cast<Window_Win32*>(w)->GetHWND();

        RegisterRawInputDevices(&mRID, 1, sizeof(mRID));
    }

    Keyboard_Win32::~Keyboard_Win32()
    {
    }

    void Keyboard_Win32::Update(Window *w)
    {
        if (mIsAwake)
        {
            RAWINPUT *raw = static_cast<Window_Win32*>(w)->GetRawInput();
            DWORD key = (DWORD)raw->data.keyboard.VKey;

            if (key < 0 || key > KEYBOARD_KEY_MAX)	{
                return;
            }

            mKeyStates[key] = !(raw->data.keyboard.Flags & RI_KEY_BREAK);
        }
    }
}